package com.soriya.nestlive.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.soriya.nestlive.common.GlobalThreadLocal;
import com.soriya.nestlive.entity.PageResult;
import com.soriya.nestlive.entity.UserEntity;
import com.soriya.nestlive.entity.param.LiveListParam;
import com.soriya.nestlive.entity.param.LoginParam;
import com.soriya.nestlive.entity.param.UserListParam;
import com.soriya.nestlive.entity.vo.InfoVo;
import com.soriya.nestlive.mapper.UserMapper;
import com.soriya.nestlive.service.UserService;
import com.soriya.nestlive.util.JwtUtil;
import com.soriya.nestlive.util.ToolsUtil;
import com.soriya.nestlive.util.excpetion.MyException;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, UserEntity> implements UserService {

    @Override
    public PageResult<UserEntity> pageQuery(Page<UserEntity> page, UserListParam param) {
        page(page);
        return PageResult.from(page);
    }

    @Override
    public boolean save(UserEntity entity) {

        LambdaQueryWrapper<UserEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserEntity::getAccount, entity.getAccount());
        UserEntity one = getOne(wrapper);
        if (one != null) throw new MyException("账户名称已被使用");

        String password = entity.getPassword();

        String salt = ToolsUtil.randomString(5);

        entity.setPassword(ToolsUtil.makeMd5(password + salt));
        entity.setSalt(salt);

        return super.save(entity);
    }

    @Override
    public boolean updateById(UserEntity entity) {

        String password = entity.getPassword();
        String salt = entity.getSalt();

        entity.setPassword(ToolsUtil.makeMd5(password + salt));

        return super.updateById(entity);
    }

    @Override
    public String login(LoginParam login) {
        String username = login.getUsername();
        String password = login.getPassword();

        System.out.println(login);

        LambdaQueryWrapper<UserEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserEntity::getAccount, username);

        UserEntity one = getOne(wrapper);
        if (one == null) throw new MyException("用户名或密码错误");

        if (!ToolsUtil.makeMd5(password + one.getSalt()).equals(one.getPassword())) throw new MyException("用户名或密码错误");

        return JwtUtil.getToken(one.getId(), one.getNickname());
    }

    @Override
    public UserEntity getInfo() {
        Long userId = GlobalThreadLocal.getUserId(); // 获取userId

        // 根据ID获取用户信息
        UserEntity userEntity = getById(userId);
        userEntity.setPassword(null);

        // 封装InfoVo
        InfoVo infoVo = new InfoVo();
        infoVo.setName("admin");
        infoVo.setRoles("[admin]");
        infoVo.setAvatar(null);

        return userEntity;
    }
}
